﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;

namespace CMD.BAL.AUTH
{
    [DataContractAttribute]
    public class Group
    {
        [DataMemberAttribute]
        public string groupMail;
        [DataMemberAttribute]
        public string groupName;
        [DataMemberAttribute]
        public string idGroup;
        public static CMD.BAL.MISC.DeleteStateMsg Delete(string[] aIdGroup)
        {
            CMD.BAL.MISC.DeleteStateMsg delete;
            //convirto
            string join = String.Join(", ", aIdGroup);
            //ejecuto el store
            try
            {
                int dt = CMD_DKT.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_DELETEGROUP'" + join + "'");
                delete = new MISC.DeleteStateMsg("registros eliminados " + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new MISC.DeleteStateMsg("error al eliminar" + ex, false);
                return delete;
            }
        }
        public static List<Group> GetData(string[] aIdGroup)
        {
            string join = String.Join(", ", aIdGroup);
            DataTable dt = CMD_DKT.DAL.SrvDB.ExecuteQuery("exec SP_CMD_SEL_GROUP'" + join + "'");
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                List<Group> list = dt.AsEnumerable().Select(
                    row => new Group
                    {
                        groupName = String.IsNullOrEmpty(row.Field<string>(1))
                        ? ""
                        : row.Field<string>(1),
                        groupMail = String.IsNullOrEmpty(row.Field<string>(2))
                        ? ""
                        : row.Field<string>(2),
                        idGroup = String.IsNullOrEmpty(row.Field<string>(0))
                        ? ""
                        : row.Field<string>(0),
                    }
                    ).ToList();
                return list;
            }
        }
        public static Group GetData(string aIdGroup)
        {
            Group group;
             DataTable dt = CMD_DKT.DAL.SrvDB.ExecuteQuery("exec SP_CMD_SEL_GROUP'" + aIdGroup + "'");
             if (dt.Rows.Count == 0)
             {
                 return null;
             }
             else
             {
                 group = new Group(dt.Rows[0]["DSC_SECGROUP"].ToString(),dt.Rows[0]["GROUPMAIL"].ToString(), dt.Rows[0]["ID_SECGROUP"].ToString());
             }
             return group;
        }
        public static CMD.BAL.MISC.UpsertStateMsg Upsert(Group data)
        {
            Group inser;
            CMD.BAL.MISC.UpsertStateMsg insertGroup;
            try
            {
                int dt = CMD_DKT.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_UPSERT_GROUP'" + data.groupName + "','" + data.groupMail + "','" + data.idGroup + "'");
                inser = new Group(data.groupName, data.groupMail, data.idGroup);
                insertGroup = new MISC.UpsertStateMsg("registros guardados " + dt.ToString(), true);
                return insertGroup;
            }
            catch (Exception ex)
            {
                insertGroup = new MISC.UpsertStateMsg("error al guardar " + ex, false);
                return insertGroup;
            }
        }
        public Group()
        {
            groupMail = "";
            groupName = "";
            idGroup = "";
        }
        public Group(string groupName,string groupMail, string idGroup) 
        {
            this.groupName =groupName;
            this.groupMail =groupMail;
            this.idGroup = idGroup;
        }
    }
}
